package com.kingoe.mybatisdj.shard.strategy;

import cn.hutool.core.date.DateUtil;
import com.kingoe.mybatisdj.shard.interceptor.ITableShardStrategy;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 分表策略-日期
 * @author jinwu
 */
@Slf4j
@Component
public class TableShardStrategyDate implements ITableShardStrategy {

    private static final String DATE_PATTERN = "yyyyMM";

    @Override
    public String generateTableName(String tableNamePrefix, Object value) {
        verificationTableNamePrefix(tableNamePrefix);
        if (value == null || StringUtils.isBlank(value.toString())) {
            return tableNamePrefix + "_" + DateUtil.format(new Date(), DATE_PATTERN);
        } else {
            return tableNamePrefix + "_" +DateUtil.format(new Date(), value.toString());
        }
    }

}
